package cn.iocoder.yudao.module.erp.service.contractfilerelation;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contract.contractfilerelation.vo.ContractFileRelationPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contract.contractfilerelation.vo.ContractFileRelationSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractfilerelation.ContractFileRelationDO;
import cn.iocoder.yudao.module.erp.dal.mysql.contractfilerelation.ContractFileRelationMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.CONTRACT_FILE_RELATION_NOT_EXISTS;

/**
 * 合同文件关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ContractFileRelationServiceImpl implements ContractFileRelationService {

    @Resource
    private ContractFileRelationMapper contractFileRelationMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createContractFileRelation(ContractFileRelationSaveReqVO createReqVO) {
        // 插入
        ContractFileRelationDO contractFileRelation = BeanUtils.toBean(createReqVO, ContractFileRelationDO.class);
        contractFileRelationMapper.insert(contractFileRelation);
        // 返回
        return contractFileRelation.getId();
    }

    @Override
    public void updateContractFileRelation(ContractFileRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateContractFileRelationExists(updateReqVO.getId());
        // 更新
        ContractFileRelationDO updateObj = BeanUtils.toBean(updateReqVO, ContractFileRelationDO.class);
        contractFileRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteContractFileRelation(Long id) {
        // 校验存在
        validateContractFileRelationExists(id);
        // 删除
        contractFileRelationMapper.deleteById(id);
    }

    @Override
    public void deleteContractFileRelationListByIds(List<Long> ids) {
        // 校验存在
        validateContractFileRelationExists(ids);
        // 删除
        contractFileRelationMapper.deleteByIds(ids);
    }

    private void validateContractFileRelationExists(List<Long> ids) {
        List<ContractFileRelationDO> list = contractFileRelationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONTRACT_FILE_RELATION_NOT_EXISTS);
        }
    }

    private void validateContractFileRelationExists(Long id) {
        if (contractFileRelationMapper.selectById(id) == null) {
            throw exception(CONTRACT_FILE_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public ContractFileRelationDO getContractFileRelation(Long id) {
        return contractFileRelationMapper.selectById(id);
    }

    @Override
    public PageResult<ContractFileRelationDO> getContractFileRelationPage(ContractFileRelationPageReqVO pageReqVO) {
        PageResult<ContractFileRelationDO> pageResult = contractFileRelationMapper.selectPage(pageReqVO);
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(pageResult.getList(),
                copy -> {
                    if (copy.getCreator() != null && !copy.getCreator().isEmpty()) {
                        return Stream.of(Long.parseLong(copy.getCreator()));
                    } else {
                        return Stream.empty();
                    }
                }));

        pageResult.getList().forEach(item -> {
            if (StrUtil.isNotBlank(item.getCreator())){
                AdminUserRespDTO adminUserRespDTO = userMap.get(Long.parseLong(item.getCreator()));
                item.setCreator(adminUserRespDTO.getNickname());
            }
        });

        return pageResult;
    }

    @Override
    public void deleteByContractId(Long contractId) {
        contractFileRelationMapper.deleteByContractId(contractId);
    }

    @Override
    public void deleteByContractIds(List<Long> contractIds) {
        contractFileRelationMapper.deleteByContractIds(contractIds);
    }

    @Override
    public List<ContractFileRelationDO> getByContractIdAndFileType(Long contractId, String fileCategory) {
        return contractFileRelationMapper.selectByContractIds(contractId,fileCategory);
    }

}